/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.source.controller;

import com.wisnu.spms.service.entity.DbGraduatestatus;
import com.wisnu.spms.service.entity.DbStudent;
import com.wisnu.spms.source.model.PanelStudentModel;
import com.wisnu.spms.source.view.PanelBilling;
import com.wisnu.spms.source.view.PanelSearchList;
import com.wisnu.spms.source.view.PanelStudent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Wisnu Wardoyo
 */
public class PanelStudentController {

    private final PanelStudentModel model;

    public PanelStudentController(PanelStudentModel model) {
        this.model = model;
    }

    public void searchById(Integer id) {
        model.setId(id);
        try {
            model.searchStudentById();
        } catch (Exception ex) {

        }
    }

    public void searchByKeyword(String keyword) {
        model.setKeyword(keyword);
        try {
            PanelSearchList panelSearchList = new PanelSearchList(model.searchStudentByKeyword());
            JOptionPane.showMessageDialog(null, panelSearchList, "Hasil Pencarian", JOptionPane.CLOSED_OPTION);
            model.setStudent(panelSearchList.getStudent());
        } catch (Exception ex) {

        }
    }

    public void editSection(PanelStudent panelStudent, Integer section) {
        try {
            switch (section) {
                case 0:
                    model.getStudent().setStudentName(panelStudent.getTxtName().getText());
                    model.updateStudent();
                    break;
                case 1:
                    model.getStudent().setStudentParent(panelStudent.getTxtParent().getText());
                    model.updateStudent();
                    break;
                case 2:
                    model.getStudent().setStudentContact1(panelStudent.getTxtContact1().getText());
                    model.updateStudent();
                    break;
                case 3:
                    model.getStudent().setStudentContact2(panelStudent.getTxtContact2().getText());
                    model.updateStudent();
                    break;
                case 4:
                    model.getStudent().setStudentAddress(panelStudent.getTxtAddress().getText());
                    model.updateStudent();
                    break;
                default:
                    break;
            }
        } catch (Exception e) {

        }
    }

    public void showBilling(PanelStudent panelStudent) {
        PanelBilling panelBilling = new PanelBilling(model.getStudent());
        panelStudent.getScrollPaneEditor().setViewportView(panelBilling);
    }

    public void addStudent(PanelStudent panelStudent) {
        String stringId = panelStudent.getTxtAddId().getText();
        DbGraduatestatus dbGraduatestatus = new DbGraduatestatus(panelStudent.getComAddStatus().getSelectedIndex()+1, "");
        String studentName = panelStudent.getTxtAddName().getText();
        String studentAddress = panelStudent.getTxtAddAddress().getText();
        String stringStudentSemester = panelStudent.getTxtAddSemester().getText();
        String studentParent = panelStudent.getTxtAddParent().getText();
        String studentContact1 = panelStudent.getTxtAddContact1().getText();
        String studentContact2 = panelStudent.getTxtAddContact2().getText();

        if (stringId.trim().equals("")) {

        } else if (stringId.length() > 10) {

        } else if (studentName.trim().equals("")) {

        } else if (studentName.length() > 255) {

        } else if (studentAddress.trim().equals("")) {

        } else if (stringStudentSemester.trim().equals("")) {

        } else if (stringStudentSemester.length() > 2) {

        } else if (studentParent.trim().equals("")) {

        } else if (studentParent.length() > 255) {

        } else if (studentContact1.trim().equals("")) {

        } else if (studentContact1.length() > 20) {

        } else {
            try {
                Integer studentId = Integer.parseInt(stringId.trim());
                Integer studentSemester = Integer.parseInt(stringStudentSemester.trim());
                
                DbStudent student = new DbStudent();
                student.setStudentId(studentId);
                student.setStudentName(studentName);
                student.setStudentSemester(studentSemester);
                student.setDbGraduatestatus(dbGraduatestatus);
                student.setStudentAddress(studentAddress);
                student.setStudentParent(studentParent);
                student.setStudentContact1(studentContact1);
                student.setStudentContact2(studentContact2);
                
                model.setStudent(student);
                model.addStudent();
                
            } catch (NumberFormatException nfe) {

            } catch (Exception ex) {
                
            }
        }

    }

}
